/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdlib.h>

#include "gl.h"

#define GLCLIENT
#include "gl_internal.h"

void glMatrixMode(GLenum mode) {
    //TODO: Add checking of mode parameter.
    current_state->stack_type = mode;
}

void glLoadIdentity() {
    if(current_state->stack_type == GL_MODELVIEW) {
        current_state->mvs->m = Matrix_identity();
    } else {
        current_state->pjs->m = Matrix_identity();
    }
}

void glLoadMatrixf(const GLfloat* v) {
    if(current_state->stack_type == GL_MODELVIEW) {
        int i;
        for(i=16; i>=0; i--) {
            current_state->mvs->m.e[i] = Fixed_fromFloat(v[i]);
        }
    } else {
        int i;
        for(i=16; i>=0; i--) {
            current_state->pjs->m.e[i] = Fixed_fromFloat(v[i]);
        }
    }
}

void glPushMatrix() {
    matrixstack* ms = (matrixstack*)malloc(sizeof(matrixstack));

    if(current_state->stack_type == GL_MODELVIEW) {
        ms->m = current_state->mvs->m;
        ms->next = current_state->mvs;
        current_state->mvs = ms;
    } else {
        ms->m = current_state->pjs->m;
        ms->next = current_state->pjs;
        current_state->pjs = ms;
    }
}

void glPopMatrix() {
    if(current_state->stack_type == GL_MODELVIEW) {
        matrixstack* ms = current_state->mvs->next;
        free(current_state->mvs);
        current_state->mvs = ms;
    } else {
        matrixstack* ms = current_state->pjs->next;
        free(current_state->pjs);
        current_state->pjs = ms;
    }
}
